package com.peng.leetcode.search;

import java.util.ArrayList;
import java.util.List;

/**
 * MinNumberInRotateArray
 * BM21 旋转数组的最小数字
 * https://www.nowcoder.com/practice/9f3231a991af4f55b95579b44b7a01ba?tpId=295&tags=&title=&difficulty=0&judgeStatus=0&rp=0&sourceUrl=%2Fexam%2Foj
 *
 * @author: lupeng6
 * @create: 2023/3/6 13:48
 */
public class MinNumberInRotateArray {
    // 5,6,7,8,9,10,1,2,3,4
    public static void main(String[] args) {
        int[] arr = new int[]{5,6,7,8,9,10,1,2,3,4};
        System.out.println(new MinNumberInRotateArray().minNumberInRotateArray(arr));
    }

    public int minNumberInRotateArray(int[] array) {
        int l = 0;
        int r = array.length - 1;
        while (l < r) {
            int mid = l + (r - l) / 2;
            if (array[mid] > array[r]) {
                l = mid + 1;
            } else if (array[mid] < array[r]) {
                r = mid;
            } else {
                r--;
            }
        }
        return array[l];
    }
}
